package com.example.myactionbardemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;

public class MainActivity extends ActionBarActivity implements  ActionBar.OnNavigationListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_main);
		
		 ActionBar actionBar = getSupportActionBar();
		    actionBar.setDisplayHomeAsUpEnabled(true);
		   actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
//		    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		    actionBar.setDisplayShowTitleEnabled(false);

		    Tab tab = actionBar.newTab()
		                       .setText("tab1")
		                       .setTabListener(new TabListener<ArtistFragment>(
		                               this, "artist", ArtistFragment.class));
		    actionBar.addTab(tab);

		    tab = actionBar.newTab()
		                   .setText("album")
		                   .setTabListener(new TabListener<AlbumFragment>(
		                           this, "album", AlbumFragment.class));
		    actionBar.addTab(tab);
		    tab = actionBar.newTab()
	                   .setText("album2")
	                   .setTabListener(new TabListener<AlbumFragment>(
	                           this, "alb2um", AlbumFragment.class));
		    actionBar.addTab(tab);
		    tab = actionBar.newTab()
	                   .setText("album3")
	                   .setTabListener(new TabListener<AlbumFragment>(
	                           this, "albu3m", AlbumFragment.class));
		    actionBar.addTab(tab);
		    tab = actionBar.newTab()
	                   .setText("album4")
	                   .setTabListener(new TabListener<AlbumFragment>(
	                           this, "alb4um", AlbumFragment.class));
		    actionBar.addTab(tab);
		    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list,
		            android.R.layout.simple_spinner_dropdown_item);
		    actionBar.setListNavigationCallbacks(mSpinnerAdapter, null);

	
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_activity_actions, menu);
		// Set up ShareActionProvider's default share intent
	    MenuItem shareItem = menu.findItem(R.id.action_compose);
	    ShareActionProvider  mShareActionProvider = (ShareActionProvider)
	            MenuItemCompat.getActionProvider(shareItem);
	    mShareActionProvider.setShareIntent(getDefaultIntent());
		
		return true;
	}
	private Intent getDefaultIntent() {
	    Intent intent = new Intent(Intent.ACTION_SEND);
	    intent.setType("image/*");
	    return intent;
	}
	public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
	    private Fragment mFragment;
	    private final Activity mActivity;
	    private final String mTag;
	    private final Class<T> mClass;

	    /** Constructor used each time a new tab is created.
	      * @param activity  The host Activity, used to instantiate the fragment
	      * @param tag  The identifier tag for the fragment
	      * @param clz  The fragment's Class, used to instantiate the fragment
	      */
	    public TabListener(Activity activity, String tag, Class<T> clz) {
	        mActivity = activity;
	        mTag = tag;
	        mClass = clz;
	    }

	    /* The following are each of the ActionBar.TabListener callbacks */

	    public void onTabSelected(Tab tab, FragmentTransaction ft) {
	        // Check if the fragment is already initialized
	        if (mFragment == null) {
	            // If not, instantiate and add it to the activity
	            mFragment = Fragment.instantiate(mActivity, mClass.getName());
	            ft.add(android.R.id.content, mFragment, mTag);
	        } else {
	            // If it exists, simply attach it in order to show it
	            ft.attach(mFragment);
	        }
	    }

	    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	        if (mFragment != null) {
	            // Detach the fragment, because another one is being attached
	            ft.detach(mFragment);
	        }
	    }

	    public void onTabReselected(Tab tab, FragmentTransaction ft) {
	        // User selected the already selected tab. Usually do nothing.
	    }
	}
	@Override
	public boolean onNavigationItemSelected(int arg0, long arg1) {
		// TODO Auto-generated method stub
		return false;
	}
}
